import openpyxl


class counting_man:
    def __init__(self, xlsx_name, sheet_name):
        self.xlsx_name = xlsx_name
        self.sheet_name = sheet_name
        self.wb = None
        self.ws = None

    def get_workbook(self):
        self.wb = openpyxl.load_workbook(self.xlsx_name)
        self.ws = self.wb[self.sheet_name]

    def get_col(self, target_name):  # 参数名修正
        """通用列定位方法"""
        for cell in self.ws[1]:  # 遍历第一行
            if cell.value == target_name:  # 使用参数而非self属性
                return cell.column
        raise ValueError(f"列名'{target_name}'未找到")

    def counting(self):
        lower_18_buy = 0
        lower_18_not_buy = 0
        between_18_and_25_buy = 0
        between_18_and_25_not_buy = 0
        between_26_and_35_buy = 0
        between_26_and_35_not_buy = 0
        between_36_and_45_buy = 0
        between_36_and_45_not_buy = 0
        upper_46_buy = 0
        upper_46_not_buy = 0
        buy_col = self.get_col('buy')
        age_col = self.get_col('age')

        for row in self.ws.iter_rows(min_row=2):
            buy_value = row[buy_col - 1].value  # 注意openpyxl列号从1开始，列表索引从0开始
            age_value = row[age_col - 1].value

            # 确保比较的值与数据实际内容一致（如'是'和'18岁以下'是否准确）
            if buy_value == '是' and age_value == '18岁以下':
                lower_18_buy += 1

            if buy_value == '否' and age_value == '18岁以下':
                lower_18_not_buy += 1

            if buy_value == '是' and age_value == '18~25岁':
                between_18_and_25_buy += 1

            if buy_value == '否' and age_value == '18~25岁':
                between_18_and_25_not_buy += 1

            if buy_value == '是' and age_value == '26~35岁':
                between_26_and_35_buy += 1

            if buy_value == '否' and age_value == '26~35岁':
                between_26_and_35_not_buy += 1

            if buy_value == '是' and age_value == '36~45岁':
                between_36_and_45_buy += 1

            if buy_value == '否' and age_value == '36~45岁':
                between_36_and_45_not_buy += 1

            if buy_value == '是' and age_value == '46岁及以上':
                upper_46_buy += 1

            if buy_value == '否' and age_value == '46岁及以上':
                upper_46_not_buy += 1

        print('低于18岁并且买预制', lower_18_buy)
        print('低于18岁并且不买预制', lower_18_not_buy)
        print('高于18岁低于25岁且买预制', between_18_and_25_buy)
        print('高于18岁低于25岁且不买预制', between_18_and_25_not_buy)
        print('高于25岁低于35岁且买预制', between_26_and_35_buy)
        print('高于25岁低于35岁且不买预制', between_26_and_35_not_buy)
        print('高于35岁低于45岁且买预制', between_36_and_45_buy)
        print('高于35岁低于45岁且不买预制', between_36_and_45_not_buy)
        print('高于46岁且买预制', upper_46_buy)
        print('高于46岁且买预制', upper_46_not_buy)
        print('总和',lower_18_buy+lower_18_not_buy+between_18_and_25_buy+between_18_and_25_not_buy+between_26_and_35_buy+between_26_and_35_not_buy+between_36_and_45_buy+between_36_and_45_not_buy+upper_46_buy+upper_46_not_buy)

    def start_counting(self):
        self.get_workbook()
        self.counting()


if __name__ == '__main__':
    xlsx_name = 'clear_maded_food.xlsx'
    sheet_name = '预制表格'
    count = counting_man(xlsx_name=xlsx_name, sheet_name=sheet_name)
    count.start_counting()
